﻿using System;
using System.Diagnostics;
using PostSharp.Laos;

namespace EasyDLL.AOP.Attributes
{
    /// <summary>
    /// 计算方法执行的时间
    /// 该attribute依赖于postsharp编译
    /// </summary>
    [Serializable]
    public class ExecuteTimeAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            eventArgs.MethodExecutionTag = Stopwatch.StartNew();
            base.OnEntry(eventArgs);
        }

        public override void OnExit(MethodExecutionEventArgs eventArgs)
        {
            Stopwatch sw = (Stopwatch)eventArgs.MethodExecutionTag;
            sw.Stop();
            string msg = string.Format("{0},{1}",eventArgs.Method.Name, sw.ElapsedMilliseconds);
            NativeAPIWrapper.Send("ExecuteTimeMonitor", msg);

            base.OnExit(eventArgs);
        }
    }
}
